﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace ElectricDim
{
	public class WireNumDim
	{
		public WireNumDim(UIDocument uiDoc)
		{
			this.m_uiDoc = uiDoc;
			this.m_doc = uiDoc.ActiveView.Document;
		}

		public void DoDim()
		{
			WireNumDimForm wireNumDimForm = new WireNumDimForm();
			wireNumDimForm.Show(WindowHelper.HWndRevit);
			WindowHelper.SendMessageToWndRevit();
			Transaction transaction = null;
			try
			{
				for (;;)
				{
					Reference reference = this.m_uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new WireFilter());
					if (reference != null)
					{
						Element element = this.m_doc.GetElement(reference);
						if (element == null)
						{
							goto IL_15D;
						}
						Wire wire = element as Wire;
						if (wire == null)
						{
							goto IL_15D;
						}
						int dimStyle = wireNumDimForm.DimStyle;
						int numType = wireNumDimForm.NumType;
						string num = wireNumDimForm.Num;
						transaction = new Transaction(this.m_doc, "MarkWireNum");
						transaction.Start();
						ElementIdDate elementIdDate = ExtensibleStorageManager.ReadFromElement<ElementIdDate>(element, this.m_guid);
						if (elementIdDate.GetId().IntegerValue != 0)
						{
							this.m_doc.Delete(elementIdDate.GetId());
						}
						if (dimStyle == 0)
						{
							element.GetParameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set(num);
						}
						double angle = ElectricalUtils.PromtAngle(reference.GlobalPoint, wire);
						Autodesk.Revit.DB.ElementType elementType = this.GetElementType(angle, dimStyle, num);
						if (elementType == null)
						{
							break;
						}
						IndependentTag independentTag = RevitVersionFuncs.NewTag(this.m_doc, this.m_doc.ActiveView, new Reference(element), false, 0, 0, reference.GlobalPoint);
						independentTag.ChangeTypeId(elementType.Id);
						transaction.Commit();
						ElementIdDate obj = new ElementIdDate(independentTag.Id);
						ExtensibleStorageManager.WriteToElement(element, this.m_guid, obj);
					}
				}
				TaskDialog.Show("盈建科提示", "没有找到标记族。");
				transaction.RollBack();
				IL_15D:;
			}
			catch (Exception)
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			wireNumDimForm.Close();
		}

		public Autodesk.Revit.DB.ElementType GetElementType(double angle, int dimStyle, string numText)
		{
			List<DbFamilyObject> list;
			if (dimStyle == 1)
			{
				int num;
				if (!int.TryParse(numText, out num))
				{
					return null;
				}
				if (num == 2)
				{
					list = ElectricMark.WireNumMarkOfTwo(this.m_doc);
				}
				else
				{
					list = ElectricMark.WireNumMarkOfThree(this.m_doc);
				}
			}
			else
			{
				list = ElectricMark.WireNumMarkOfMore(this.m_doc);
			}
			if (list == null || list.Count <= 0)
			{
				return null;
			}
			DbFamilyObject dbFamilyObject = list[0];
			if (dbFamilyObject == null)
			{
				return null;
			}
			Family family = dbFamilyObject.LoadInSubTrans(this.m_doc);
			if (family == null)
			{
				return null;
			}
			string b;
			if ((angle > 0.0 && angle <= 15.0) || (angle > 180.0 && angle <= 195.0))
			{
				b = "15°";
			}
			else if ((angle > 15.0 && angle <= 30.0) || (angle > 195.0 && angle <= 210.0))
			{
				b = "30°";
			}
			else if ((angle > 30.0 && angle <= 45.0) || (angle > 210.0 && angle <= 225.0))
			{
				b = "45°";
			}
			else if ((angle > 45.0 && angle <= 60.0) || (angle > 225.0 && angle <= 240.0))
			{
				b = "60°";
			}
			else if ((angle > 60.0 && angle <= 75.0) || (angle > 240.0 && angle <= 255.0))
			{
				b = "75°";
			}
			else if ((angle > 75.0 && angle <= 90.0) || (angle > 255.0 && angle <= 270.0))
			{
				b = "90°";
			}
			else if ((angle > 90.0 && angle <= 105.0) || (angle > 270.0 && angle <= 285.0))
			{
				b = "-75°";
			}
			else if ((angle > 105.0 && angle <= 120.0) || (angle > 285.0 && angle <= 300.0))
			{
				b = "-60°";
			}
			else if ((angle > 120.0 && angle <= 135.0) || (angle > 300.0 && angle <= 315.0))
			{
				b = "-45°";
			}
			else if ((angle > 135.0 && angle <= 150.0) || (angle > 315.0 && angle <= 330.0))
			{
				b = "-30°";
			}
			else if ((angle > 165.0 && angle <= 180.0) || (angle > 345.0 && angle <= 360.0))
			{
				b = "0°";
			}
			else
			{
				b = "0°";
			}
			Autodesk.Revit.DB.ElementType result = null;
			foreach (Autodesk.Revit.DB.ElementType elementType in family.GetSymbols())
			{
				if (elementType.Name == b)
				{
					result = elementType;
					break;
				}
			}
			return result;
		}

		private Guid m_guid = new Guid("356cda9a-e6c5-4c2f-a9ef-b1313131b8c8");

		private UIDocument m_uiDoc;

		private Document m_doc;
	}
}
